class Count {
    private int count = 0;
    //声明一个计数器
    public void add() {
        synchronized(this) {
            count++;
        }
    }
    public int show() {
        return count;
    }
}

public class ThreadDemo10 {
    public static void main(String[] args) throws InterruptedException {

        Count count = new Count();
        //new一个Count对象
        //分别在两个线程中进行自增5w次
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count.add();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count.add();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(count.show());
    }
}
